﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Castle.ActiveRecord;
using System.Text;
using SimplePMT.Data;
using NHibernate.Expression;

namespace SimplePMT.Ajax
{
    public static class Projects
    {

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]

        public static string GetProjectTreeNodeAll(int parentContainerID)
        {
            StringBuilder output = new StringBuilder();
            IList<TaskContainer> containers = TaskContainer.FindAllByProperty("ParentContainerID", parentContainerID);
            output.Append(GetProjectTreeNode(parentContainerID, true));
            return output.ToString();
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static string GetProjectTreeNode(int parentContainerID, bool showExpanded)
        {
            bool ReadOnly = false;
            if (HttpContext.Current.Session["MemberID"] != null) {
                Member member = Member.Find(Convert.ToInt32(HttpContext.Current.Session["MemberID"]));
                ReadOnly = member.ReadOnly;
            }
            StringBuilder projectJsArray = new StringBuilder();
            StringBuilder output = new StringBuilder();
            using (new SessionScope())
            {
                IList<TaskContainer> containerList = TaskContainer.FindAllByProperty("ParentContainerID", parentContainerID);
                projectJsArray.Append("<script>var projectArray = new Array();\r\n");
                output.Append("<table width=100% cellspacing=0 style=\"border:dotted 1px #869AAE\">\r\n");
                int f = 0;
                foreach (TaskContainer container in containerList)
                {
                    if (parentContainerID.Equals(0))
                    {
                        projectJsArray.Append("projectArray[" + f + "] = [" + container.ID + ", document.img" + container.ID + "];\r\n");
                        f++;
                    }
                    int itemCount = CheckContainerCount(container.ID);
                    int openItemCount = CheckContainerOpenCount(container.ID);

                    output.Append("<tr onmouseover=\"this.className='highlightRow';showContainerOptions(" + container.ID + ");\" onmouseout=\"this.className='';hideContainerOptions();\">\r\n");
                    output.Append("<td height=17 align=right OnClick=\"javascript:OnClick=loadTree(" + container.ID + ", document.img" + container.ID + ");\">\r\n");
                    output.Append("<img hspace=3 vspace=3 src=\"images/");
                    if (showExpanded) {
                        output.Append("open.gif");
                    } else {
                        output.Append("closed.gif");
                    }
                    output.Append("\" name=\"img" + container.ID + "\" border=0/>&nbsp;");

                    output.Append("</td>\r\n");
                    output.Append("<td style=\"white-space:nowrap;\">\r\n");
                    if (itemCount > 0)
                    {
                        output.Append("<span style=\"white-space:nowrap;\" OnClick=\"javascript:OnClick=loadTree(" + container.ID + ", document.img" + container.ID + ");\">\r\n");    
                    }
                    output.Append(container.Label + "</span> (<span id=\"ClosedItemCount" + container.ID + "\" style=\"display:none\">" + itemCount + "</span><span OnClick=\"javascript:OnClick=loadTree(" + container.ID + ", document.img" + container.ID + ");\" id=\"OpenItemCount" + container.ID + "\">" + openItemCount + "</span>)<span id=\"options" + container.ID + "\" style=\"display:none;width:150px;\">&nbsp;&nbsp;&nbsp;");
                    if (!ReadOnly) {
                        output.Append("<a href=\"javascript:OnClick=openContainer(" + container.ParentContainerID + ", " + container.ID + ");\" class=\"hrefTree\">edit</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:OnClick=openContainer(" + container.ID + ", 0);\" class=\"hrefTree\">add folder</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:OnClick=openTask(0, " + container.ID + ", 'Add New Task');\" class=\"hrefTree\">add task</a>&nbsp;&nbsp;&nbsp;<a href=\"javascript:OnClick=openFile(0, " + container.ID + ", 'Add New File Attachment');\" class=\"hrefTree\">add file</a>");
                    }
                    output.Append("</span></td>\r\n");
                    output.Append("</tr>\r\n");
                    output.Append("<tr>\r\n");
                    output.Append("<td width=19 align=right></td>\r\n");
                    output.Append("<td id=\"container" + container.ID + "\"");
                    if (!showExpanded) {
                        output.Append(" style=\"display:none\"");
                    }
                    output.Append(">\r\n");
                    if (showExpanded) {
                        output.Append(GetProjectTreeNode(container.ID, showExpanded));
                    }
                    output.Append("</td>\r\n");
                    output.Append("</tr>\r\n");
                    output.Append("<tr>\r\n");
                    output.Append("<td></td>\r\n");
                    output.Append("<td id=\"taskList" + container.ID + "\"");
                    if (!showExpanded) {
                        output.Append(" style=\"display:none\"");
                    }
                    output.Append(">");
                    if (container.Attachments.Count > 0 || container.Items.Count > 0)
                    {
                        output.Append("<table width=100% cellspacing=0 cellpadding=2 border=0>\r\n");
                    }

                    foreach (TaskContainerAttachment att in container.Attachments)
                    {

                        output.Append("<tr onmouseover=\"this.className='highlightRow';showAttachmentOptions(" + att.Attachment.ID + ");\" onmouseout=\"this.className='';hideAttachmentOptions();\">\r\n");
                        
                        output.Append("<td height=17 colspan=2 id=\"attachmentid" + att.Attachment.ID + "\" style=\"white-space:nowrap;\"");
                        output.Append("><img src=\"images/file.png\"> <a class=\"hrefTree\" href=\"attachments/"+ att.Attachment.Path + "\" target=\"_new\"><b>" + att.Attachment.Title + "</b>\r\n");
                        if (att.Attachment.MemberID > 0)
                        {
                            Member member = Member.Find(att.Attachment.MemberID);
                            output.Append(" (added by " + member.FirstName + " " + member.LastName + " " + att.Attachment.DateTime.Value.ToShortDateString() + ")");
                        }
                        output.Append("</a><span id=\"attachmentOptions" + att.Attachment.ID + "\" style=\"display:none;width:150px;\">");
                        if (!ReadOnly)
                        {
                            output.Append("&nbsp;&nbsp;&nbsp;<a class=\"hrefTree\" href=\"javascript:OnClick=openFile(" + att.Attachment.ID + ", " + container.ID + ", '" + att.Attachment.Title.Replace("\"", "") + "');\">edit</a>\r\n");
                        }
                        output.Append("</span></td>\r\n");
                        output.Append("</tr>\r\n");
                    }

                    foreach (TaskContainerItem item in container.Items)
                    {
                        Task task = Task.Find(item.TaskID);
                        string taskIcon = task.TaskType.Icon;
                        bool closed = !task.ClosedDateTime.ToShortDateString().Equals("1/1/1900");

                        if (closed)
                        {
                            taskIcon = "checkmark.png";
                        }

                        output.Append("<tr id=\"taskRow" + task.ID + "\" onmouseover=\"this.className='highlightRow';\" onmouseout=\"this.className='';\" OnClick=\"openModal('TaskView.aspx?id=" + task.ID + "&cid=" + item.TaskContainer.ID + "', 'SimplePMT - " + task.Title.Replace("'", "\\'").Replace("\"", "") + "');\"");
                        if (closed) {
                            output.Append(" style=\"display:none\"");
                        }

                        output.Append(">\r\n");
                        output.Append("<td colspan=2 height=17 id=\"taskid" + task.ID + "\"");
                        if (closed) {
                            output.Append(" style=\"text-decoration:line-through;\"");
                        }

                        output.Append("><img src=\"images/" + taskIcon + "\"> <b>[" + task.ID +"] "+ task.Title + "</b> (");

                        if (task.AssignedMemberID > 0) {
                            Member member = Member.Find(task.AssignedMemberID);
                            output.Append(member.FirstName +" "+ member.LastName +" "+ task.DueDate.Value.ToShortDateString());
                        } else {
                            output.Append("unassigned");   
                        }
                        output.Append(") " + task.Priority.Label + " - " + task.TaskType.Label);
                        output.Append("</td>") ;
                        output.Append("</tr>\r\n");
                    }

                    if (container.Attachments.Count > 0 || container.Items.Count > 0)
                    {
                        output.Append("</table>");
                    }
                    output.Append("</td></tr>\r\n");
                }
                output.Append("</table>\r\n");
            }

            projectJsArray.Append("</script>\r\n");
            return output.ToString() +"\r\n"+ projectJsArray.ToString();
        }
        public static int CheckContainerCount(int id)
        {
            int count = 0;
            using (new SessionScope())
            {
                count += TaskContainer.FindAllByProperty("ParentContainerID", id).Length;
                count += TaskContainer.Find(id).Items.Count;
                count += TaskContainer.Find(id).Attachments.Count;
            }
            return count;
        }

        public static int CheckContainerOpenCount(int id)
        {
            int count = 0;
            using (new SessionScope())
            {
                TaskContainer container = TaskContainer.Find(id);
                foreach (TaskContainerItem item in container.Items)
                {
                    if (Task.Find(item.TaskID).ClosedDateTime.Equals(new DateTime(1900, 1, 1)))
                    {
                        count++;
                    }
                }
                count += TaskContainer.FindAllByProperty("ParentContainerID", id).Length;
                count += TaskContainer.Find(id).Attachments.Count;
            }
            return count;
        }
    }
}
